package com.houtai.controller.admin;


import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.Page;
import com.houtai.base.RetInfo;
import com.houtai.bean.TsJzAdmin;
import com.houtai.bean.dto.admin.AdminLogin;
import com.houtai.common.util.Ip;
import com.houtai.common.util.MD5;
import com.houtai.service.admin.AdminService;
import com.houtai.service.admin.operation.AdminOperationService;
import com.houtai.service.funcion.FuncionService;
import com.houtai.service.role.RoleService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping(value = {"v5"})
public class AdminController {
	private static Logger logger = Logger.getLogger(AdminController.class);
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private AdminService adminService;
	@Autowired
	private FuncionService funcionService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private AdminOperationService adminOperationService;
	/**
	 * @Purpose   登录
	 * @param
	 * @version   1.0
	 * @author    李寻欢
	 * @time      2018-07-09
	 * @return    RetInfo
	 */
	@RequestMapping(value="/back/to/index")
	@Authority(AuthorityType.NoValidate)
	public ModelAndView toindex(HttpServletRequest request){
		String ip = Ip.getIpAddr(request);
//		if ("183.130.234.84, 115.205.217.246".contains(ip)) {
			ModelAndView mv = new ModelAndView("admin/to");
			mv.addObject("ip", Ip.getIpAddr(request));
			return mv;
//		} else {
//			logger.info("ip不在白名单内*" + ip);
//			return null;
//		}

	}
	/**
	 * @Purpose  操作日志列表
	 * @version  1.0
	 * @author   lizhun
	 * @param    request
	 * @return   RetInfo
	 */
	@RequestMapping(value="/admin/operation/list")
	@Authority(AuthorityType.Login)
	public ModelAndView list(Page page, HttpServletRequest request){
		ModelAndView mv = new ModelAndView("admin/operation/list");
		TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
		mv.addObject("retInfo", adminOperationService.findAdminOperationPage(page, tsJzAdmin));
		return mv;
	}
	/**
	 * @Purpose   登录
	 * @param     adminLogin
	 * @version   1.0
	 * @author    李寻欢
	 * @time      2018-07-09
	 * @return    RetInfo
	 */
	@RequestMapping(value="/login")
	@ResponseBody
	@Authority(AuthorityType.NoValidate)
	public RetInfo login(AdminLogin adminLogin, HttpServletRequest request){
		adminLogin.setLogin_ip(Ip.getIpAddr(request));
		return adminService.login(adminLogin,request);
	}

	/**
	 * @Purpose   分页查找管理员
	 * @param     current_page
	 * @version   1.0
	 * @author    李寻欢
	 * @time      2018-07-09
	 * @return    RetInfo
	 */
	@Authority(AuthorityType.Login)
	@RequestMapping(value = "/admin/accounts/list")
	public ModelAndView list(@RequestParam(value = "current_page", required = true) Integer current_page, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("admin/index");
		mv.addObject("retInfo", adminService.findAdminPage(current_page, 10, request));
		mv.addObject("token", request.getParameter("token"));
		TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
		mv.addObject("funcions", funcionService.findSonFuncion(tsJzAdmin, Integer.valueOf(request.getParameter("father_id"))));
		return mv;
	}
	/**
	 * @Purpose   创建新的管理员帐号
	 * @param     tsJzAdmin
	 * @version   1.0
	 * @author    李寻欢
	 * @time      2018-07-10
	 * @return    RetInfo
	 */
	@Authority(AuthorityType.Login)
	@RequestMapping(value = "/admin/accounts/insert", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
	@ResponseBody
	public RetInfo insertAdmin(@RequestBody TsJzAdmin tsJzAdmin, HttpServletRequest request) {
		RetInfo retInfo = adminService.insertAdmin(tsJzAdmin, request);
		return retInfo;
	}

	/**
	 * @Purpose   修改管理员帐号
	 * @param     tsJzAdmin
	 * @version   1.0
	 * @author    李寻欢
	 * @time      2018-07-10
	 * @return    RetInfo
	 */
	@Authority(AuthorityType.Login)
	@RequestMapping(value = "/admin/accounts/update", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
	@ResponseBody
	public RetInfo updateAdmin(@RequestBody TsJzAdmin tsJzAdmin, HttpServletRequest request) {
		RetInfo retInfo = adminService.updateAdmin(tsJzAdmin, request);
		return retInfo;
	}

	/**
	 * @Purpose   跳转到添加管理员帐号页面
	 * @param
	 * @version   1.0
	 * @author    李寻欢
	 * @time      2018-07-10
	 * @return    ModelAndView
	 */
	@Authority(AuthorityType.Login)
	@RequestMapping(value = "/admin/accounts/insert/jump")
	public ModelAndView insert(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("admin/insert");
		mv.addObject("token", request.getParameter("token"));
		TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
		mv.addObject("roles", roleService.findRoles(tsJzAdmin));
		return mv;
	}

	/**
	 * @Purpose   跳转到更新管理员帐号页面
	 * @param
	 * @version   1.0
	 * @author    李寻欢
	 * @time      2018-07-10
	 * @return    ModelAndView
	 */
	@Authority(AuthorityType.Login)
	@RequestMapping(value = "/admin/accounts/update/jump")
	public ModelAndView update(@RequestParam(value = "admin_id", required = true) Integer admin_id, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("admin/update");
		mv.addObject("token", request.getParameter("token"));
		TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
		mv.addObject("roles", roleService.findRoles(tsJzAdmin));
		mv.addObject("admin", adminService.findAdminById(admin_id,request));
		return mv;
	}

	/**
	 * @Purpose   删除管理员
	 * @param     tsJzAdmin
	 * @version   1.0
	 * @author    李寻欢
	 * @time      2018-07-09
	 * @return    RetInfo
	 */
	@RequestMapping(value = "/admin/accounts/delete", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
	@ResponseBody
	@Authority(AuthorityType.Login)
	public RetInfo deleteAdmin(@RequestBody TsJzAdmin tsJzAdmin, HttpServletRequest request) {
		RetInfo retInfo = adminService.deleteAdmin(tsJzAdmin, request);
		return retInfo;
	}
	/**
	 * @Purpose   重置管理员密码
	 * @param     tsJzAdmin
	 * @version   1.0
	 * @author    李寻欢
	 * @time      2018-07-09
	 * @return    RetInfo
	 */
	@RequestMapping(value = "/admin/accounts/resetting/pass", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
	@ResponseBody
	@Authority(AuthorityType.Login)
	public RetInfo resettingAdminPass(@RequestBody TsJzAdmin tsJzAdmin, HttpServletRequest request) {
		tsJzAdmin.setUser_pass(MD5.code(tsJzAdmin.getUser_pass()));
		RetInfo retInfo = adminService.updateAdminPassword(tsJzAdmin, request);
		return retInfo;
	}
	/**
	 * @Purpose   修改密码
	 * @param     old_pass,user_pass
	 * @version   1.0
	 * @author    李寻欢
	 * @time      2018-07-09
	 * @return    RetInfo
	 */
	@Authority(AuthorityType.Login)
	@RequestMapping(value = "/admin/accounts/update/pass", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public RetInfo updateAdminPass(String old_pass, String user_pass, HttpServletRequest request) {
		RetInfo retInfo = new RetInfo();

		TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
		if (tsJzAdmin.getUser_pass().equals(MD5.code(old_pass))) {
			tsJzAdmin.setUser_pass(MD5.code(user_pass));
			retInfo = adminService.updateAdminPassword(tsJzAdmin, request);
		} else {
			retInfo.setMark("1");
			retInfo.setTip("旧密码不对");
		}
		return retInfo;
	}
	/**
	 * @Purpose   跳转到修改密码
	 * @param
	 * @version   1.0
	 * @author    李寻欢
	 * @time      2018-07-09
	 * @return    ModelAndView
	 */
	@Authority(AuthorityType.Login)
	@RequestMapping(value = "/admin/accounts/to/update/pass", method = {RequestMethod.GET, RequestMethod.POST})
	public ModelAndView toUpdateAdminPass(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("admin/updatePass");
		mv.addObject("token", request.getParameter("token"));
		return mv;
	}

}
